package com.AC.ACBaidu;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int group = sc.nextInt();
        for(int i = 0; i<group; i++){
            List<Integer> res ;
            int ans = 0;
            int n = sc.nextInt();
            int k = sc.nextInt();
            int[] arr = new int[n];

            for(int j = 1; j<=k; j++){
                int m = sc.nextInt();
                List<int[]> list = new ArrayList<>();

                for(int z = 0; z<m; z++){
                    int[] cur = new int[2];
                    cur[0] = sc.nextInt();
                    cur[1] = sc.nextInt();
                    list.add(cur);
                }
                build(list,arr, j);
            }

            res = getBestCow(arr, k);
            System.out.println(res.size());
            for(int j = 0; j<res.size(); j++)
                System.out.print("" + res.get(j) + "" +  (j == res.size() - 1 ? "" : " " ));
        }

    }

    public static void outArr(int[] arr){
        for(int num : arr)
            System.out.print(num + " ");
        System.out.println();
    }

    public static int[] build(List<int[]> list, int[] arr, int cur){
        for(int[] ar : list){
            int l = ar[0];
            int r = ar[1];
            for(int i = l; i<=r; i++)
                if(arr[i] != cur)
                    arr[i]++;
        }
        return arr;
    }

    public static List<Integer> getBestCow(int[] arr, int k){
        List<Integer> res = new ArrayList<>();
        for(int i = 0; i<arr.length; i++){
            if(arr[i] == k)
                res.add(i);
        }
        return res;
    }
}
